package com.dianziq.im.server;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;

public class HttpServer {
	final ChannelGroup allChannels = new DefaultChannelGroup("nio-server");

	protected ServerBootstrap bootstrap;
	protected ChannelFactory channelFactory = null;
	ConnectionService handler = new ConnectionService();

	public void init() {

	}

	protected ChannelPipelineFactory getChannelPipelineFactory() {
		return new ChannelPipelineFactory() {
			public ChannelPipeline getPipeline() throws Exception {
				// Create a default pipeline implementation.
				ChannelPipeline pipeline = Channels.pipeline();

				pipeline.addLast("decoder", new HttpRequestDecoder());
				pipeline.addLast("encoder", new HttpResponseEncoder());

				pipeline.addLast("handler", handler);

				return pipeline;

			}

		};
	}

	protected final SocketAddress getSocketAddress() {

		return new InetSocketAddress(this.defaultPort());
	}

	protected int defaultPort() {

		return Config.INSTANCE.getInt("listen-port", 80);
	}

	protected ChannelFactory createChannelFactory() {

		return new NioServerSocketChannelFactory(
				Executors.newCachedThreadPool(),
				Executors.newCachedThreadPool());

		// Executors.newCachedThreadPool(new
		// NamedThreadFactory(this.serverName()+"-work"))
		// Executors.newCachedThreadPool(),
		// new MemoryAwareThreadPoolExecutor(4, 0, 100000000)

	}

	/**
	 * start
	 * 
	 * @todo Implement this com.woyo.search.common.Server method
	 */
	public void start() {
		this.channelFactory = this.createChannelFactory();

		bootstrap = new ServerBootstrap(channelFactory);

		bootstrap.setPipelineFactory(getChannelPipelineFactory());
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);

		Channel serverChannel = bootstrap.bind(this.getSocketAddress());
		allChannels.add(serverChannel);

	}

	/**
	 * stop
	 * 
	 * @todo Implement this com.woyo.search.common.Server method
	 */
	public void stop() {
		allChannels.close().awaitUninterruptibly();
		if (channelFactory != null) {
			new Thread(" destory") {
				public void run() {
					channelFactory.releaseExternalResources();
				}
			}.start();

		}

	}
	public static void main(String[] args) {
		ConcurrentSkipListMap<Integer, String> channels = new java.util.concurrent.ConcurrentSkipListMap();
		channels.put(0, "000");
		channels.put(5, "000");
		for (Integer i : channels.keySet()) {
			System.err.println(i);
		}
	}
}